package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.util.DataUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.dao.CreditFixedLimitDao;
import com.xbongbong.saas.domain.entity.ext.CreditFixedLimitEntityExt;
import com.xbongbong.saas.enums.business.CreditFixedLimitEnum;
import com.xbongbong.saas.help.ShowFormatHelp;
import com.xbongbong.saas.model.CreditFixedLimitModel;
import com.xbongbong.saas.model.CreditFixedLimitSubDataModel;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import org.elasticsearch.action.support.WriteRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author xingxing.xiao
 */
@Service("creditFixedLimitModel")
public class CreditFixedLimitModelImpl implements CreditFixedLimitModel {

    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private CreditFixedLimitDao creditFixedLimitDao;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;
    @Resource
    private CreditFixedLimitSubDataModel creditFixedLimitSubDataModel;
    @Resource
    private ShowFormatHelp showFormatHelp;

    @Override
    public Integer insert(CreditFixedLimitEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);

        Integer success = creditFixedLimitDao.insert(entity);
        PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
        BeanUtil.copyProperties(entity, paasFormDataEntity);
        InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT);
        paasEsModel.insert(insertDTO);

        paasFormSubDataModel.insertSubFormData(entity, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);
        return success;
    }

    @Override
    public void insertBatch(List<CreditFixedLimitEntityExt> list, boolean isImport) throws XbbException {
        for (PaasFormDataEntity paasFormDataEntity : list) {
            paasFormSubDataModel.transferSubForm(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);
        }

        creditFixedLimitDao.insertBatch(list);
        //ES数据插入
        List<InsertDTO> insertList = new ArrayList<>();
        for (PaasFormDataEntity item : list) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(item.getCorpid() + "_" + item.getId());
            item.setDataId(item.getId());
            insertDTO.setSource(BeanUtil.convertBean2Map(item, true, true));
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT);
            insertList.add(insertDTO);
        }
        if (isImport) {
            paasEsModel.insertBatch(insertList, WriteRequest.RefreshPolicy.NONE);
        } else {
            paasEsModel.insertBatch(insertList);
        }

        paasFormSubDataModel.insertBatchSubFormData(list, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);
    }

    @Override
    public Integer updateBatch(List<UpdateDataEntity> list, String corpid) throws XbbException {
        //1. 先拿到子表单数据
        List<UpdateDataEntity> subList = DataUtil.getUpdateSubDataList(list);
        // 2. 主表删掉子表单数据
        DataUtil.removeSubDataInData(list);
        Integer integer = 0;
        if (CollectionsUtil.isNotEmpty(list)) {
            integer = creditFixedLimitDao.updateBatch(list, corpid);
            paasEsModel.updateBatchMuchField(list, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT);
        }
        if (!subList.isEmpty()) {
            creditFixedLimitSubDataModel.updateBatch(subList, corpid);
        }
        return integer;
    }

    @Override
    public Integer getEntityCount(Map<String, Object> param) {
        return creditFixedLimitDao.getEntitiesCount(param);
    }

    @Override
    public CreditFixedLimitEntityExt getByKey(Long key, String corpid) {
        CreditFixedLimitEntityExt entityExt = creditFixedLimitDao.getByKey(key, corpid);
        paasFormSubDataModel.findSubEntity(corpid, Collections.singletonList(entityExt), IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);
        return entityExt;
    }


    @Override
    public Integer update(CreditFixedLimitEntityExt entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        paasFormSubDataModel.transferSubForm(entity, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);
        Integer success = creditFixedLimitDao.update(entity);
        Integer updateSub = paasFormSubDataModel.updateSubFormData(entity, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);
        if (Objects.equals(1, success) || Objects.equals(1, updateSub)) {
            // 更新时间
            success = creditFixedLimitDao.updateTime(DateTimeUtil.getInt(), entity.getId(), entity.getCorpid());
            PaasFormDataEntity paasFormDataEntity = new PaasFormDataEntity();
            BeanUtil.copyProperties(entity, paasFormDataEntity);
            InsertDTO insertDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT);
            paasEsModel.update(insertDTO);
        }
        return success;
    }

    @Override
    public List<CreditFixedLimitEntityExt> findEntitys(Map<String, Object> param) {
        List<CreditFixedLimitEntityExt> entitys = creditFixedLimitDao.findEntities(param);
        Object corpid = param.get(ParameterConstant.CORPID);
        if (Objects.nonNull(corpid)) {
            paasFormSubDataModel.findSubEntity(corpid.toString(), entitys, IndexTypeEnum.IDX_SAAS_CREDIT_FIXED_LIMIT_SUB);
        }
        return entitys;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return null;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    /**
     * 编辑时将获取数据格式化
     *
     * @param saasAnalysisDataDTO saas编辑数据
     * @param result 要返回的jsonobject
     * @throws XbbException 业务异常
     */
    @Override
    public void formatData(SaasAnalysisDataDTO saasAnalysisDataDTO, JSONObject result) throws XbbException {
        showFormatHelp.formatLinkBusiness4Show(result, CreditFixedLimitEnum.CUSTOMER_ID.getAttr(), CreditFixedLimitEnum.CUSTOMER_NAME.getAttr(), CreditFixedLimitEnum.CUSTOMER_ID.getFieldType());
    }
}
